﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using FCNS.KeepMoneyLibrary.Data;

namespace FCNS.KeepMoney.Data
{
    class DataFormatXML:IDataFormat
    {
        public string Name
        {
            get { return "金钱袋 xml 格式"; }
        }

        public string Extension
        {
            get { return "(*.xml)|*.xml"; }
        }

        public List<EntryField> ImportFile(string file)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(file);
            List<EntryField> fields = new List<EntryField>();

            foreach (XmlNode node in xml.SelectNodes("KeepMoney/Entry"))
                InsertField(fields, node);

            return fields;
        }

        private void InsertField(List<EntryField> fields,XmlNode node)
        {
            //金额如果为空则丢失行,为空的账务记录是没意义的
            decimal money=0;
            if(!decimal.TryParse(node.SelectSingleNode("Money").InnerText,System.Globalization.NumberStyles.Any,null,out money))
                return;

            //如果群组 Id 不存在或为空则按金额的正负取程序默认值.
            int groupId;
            bool checkGroupId=int.TryParse(node.SelectSingleNode("GroupId").InnerText,out groupId);
            if(checkGroupId )
                checkGroupId= GroupField.GetGroupField.Find(gf=>{return gf.Id==groupId;})!=null;

            if(!checkGroupId)
                groupId=GroupField.GetDefaultGroup(money>0).Id;

            EntryField ef = new EntryField(groupId);
            ef.Money = money;
            ef.columnName = node.SelectSingleNode("columnName").InnerText;
            ef.columnNote=node.SelectSingleNode("columnNote").InnerText;
            DateTime dt;
            if(DateTime.TryParse(node.SelectSingleNode("Date").InnerText,out dt))
                ef.Date=dt;
            else
                ef.Date=DateTime.Now;

            if (DateTime.TryParse(node.SelectSingleNode("AlertDate").InnerText, out dt))
                ef.AlertDate = dt;
            else
                ef.AlertDate = DateTime.Now;

            fields.Add(ef);
        }

        public string ExampleUrl
        {
            get
            {
                return "http://www.keepmoney.org/download/example/xml.rar";
            }
        }

        public void ExportFile(List<EntryField> fields, string file)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml("<KeepMoney></KeepMoney>");
            foreach (EntryField ef in fields)
                InsertNode(xml, ef);

            xml.Save(file);
        }

        private void InsertNode(XmlDocument xml,EntryField field)
        {
            XmlElement xe = xml.CreateElement("Entry");

            XmlElement xe1 = xml.CreateElement("GroupId");
            XmlElement xe2 = xml.CreateElement("columnName");
            XmlElement xe3 = xml.CreateElement("Money");
            XmlElement xe4 = xml.CreateElement("Date");
            XmlElement xe5 = xml.CreateElement("columnNote");
            XmlElement xe6 = xml.CreateElement("AlertDate");

            xe1.InnerText = field.GroupId.ToString();
            xe2.InnerText = field.columnName;
            xe3.InnerText = field.Money.ToString();
            xe4.InnerText = field.columnDate;
            xe5.InnerText = field.columnNote;
            xe5.InnerText = field.columnAlertDate;

            xe.AppendChild(xe1);
            xe.AppendChild(xe2);
            xe.AppendChild(xe3);
            xe.AppendChild(xe4);
            xe.AppendChild(xe5);
            xe.AppendChild(xe6);

            xml.FirstChild.AppendChild(xe);
        }
    }
}
